package com.william.jdbcplus.boot.test.dao.impl;

import com.william.jdbcplus.boot.starter.wrap.QueryLambdaBootWrap;
import com.william.jdbcplus.boot.test.dao.SysMenuDataRuleDao;
import com.william.jdbcplus.boot.test.entity.*;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@AllArgsConstructor
public class SysMenuDataRuleDaoImpl implements SysMenuDataRuleDao {
    @Override
    public List<String> queryDataRuleIds(String username, String permissionId) {
        //        <!--  查询权限 TODO 这里是不是可以不关联查找 sys_role 表 -->
        //	<select id="queryDataRuleIds" resultType="java.lang.String">
        //                select data_rule_ids
        //        from sys_role_permission a
        //        join sys_permission b on a.permission_id = b.id
        //        join sys_role c on a.role_id = c.id
        //        join sys_user_role d on d.role_id = c.id
        //        join sys_user e on d.user_id = e.id
        //        where e.username = #{username} and b.id =  #{permissionId}
        //		<!--update begin Author:lvdandan  Date:20200213 for：加入部门权限 -->
        //                union
        //        select data_rule_ids
        //        from sys_depart_role_permission a
        //        join sys_permission b on a.permission_id = b.id
        //        join sys_depart_role c on a.role_id = c.id
        //        join sys_depart_role_user d on d.drole_id = c.id
        //        join sys_user e on d.user_id = e.id
        //        where e.username = #{username} and b.id = #{permissionId}
        //		<!--update end Author:lvdandan  Date:20200213 for：加入部门权限 -->
        //	</select>

        QueryLambdaBootWrap bootstrap = new QueryLambdaBootWrap();
        bootstrap
                .select(SysRoleMenu::getDataRuleIds)
                .from(SysRoleMenu.class)
                .innerJoin(SysMenu.class, SysMenu::getId, SysRoleMenu::getPermissionId)
                .innerJoin(SysRole.class, SysRole::getId, SysRoleMenu::getRoleId)
                .innerJoin(SysUserRole.class, SysUserRole::getRoleId, SysRole::getId)
                .innerJoin(SysUser.class, SysUser::getId, SysUserRole::getUserId)
                .eq(SysUser::getUsername, username)
                .eq(SysMenu::getId, permissionId)
                .union(lb -> lb
                        .select(SysDepartRoleMenu::getDataRuleIds)
                        .from(SysDepartRoleMenu.class)
                        .innerJoin(SysMenu.class, SysMenu::getId, SysDepartRoleMenu::getPermissionId)
                        .innerJoin(SysDepartRole.class, SysDepartRole::getId, SysDepartRoleMenu::getRoleId)
                        .innerJoin(SysDepartRoleUser.class, SysDepartRoleUser::getDroleId, SysDepartRole::getId)
                        .innerJoin(SysUser.class, SysUser::getId, SysDepartRoleUser::getUserId)
                        .eq(SysUser::getUsername, username)
                        .eq(SysMenu::getId, permissionId)
                );

        return bootstrap.queryList(String.class);
    }
}
